package com.finance.system.configure.websecurity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finance.system.user.entity.UserEntity;
import com.finance.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component("userNamePasswordDetailService")
public class UserNamePasswordDetailService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",s);
        UserEntity user = userMapper.selectOne(wrapper);
        if(user == null){
            return null;
        }
        return User.withUsername(user.getUserName())
                .password(user.getPassword())
                .authorities(Collections.emptyList()).build();
    }
}
